package lnu.project.cart.provider;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;

import lnu.project.cart.content.BasketContentProvider;
import lnu.project.cart.entity.Basket;
import lnu.project.cart.entity.Product;

public class ProductProvider implements IProductProvider {

	private Activity mActivity;
	private static final String[] mContent = new String[] { 
			Product.ID, // 0
			Product.BASKET_ID, // 1
			Product.TYPE_OF_PRODUCT, // 2
			Product.NAME, // 3
			Product.BARCODE, // 4
			Product.QUANTITY, // 5
			Product.WEIGHT, // 6
			Product.PRICE // 7
	};

	@Override

	public void Create(long basketId, Product p) {
		ContentValues values = new ContentValues(7);
		System.out.println("Create product");
		values.put(Product.BASKET_ID, basketId);
		values.put(Product.TYPE_OF_PRODUCT, p.getTypeOfProduct());
		values.put(Product.NAME, p.getName());
		values.put(Product.BARCODE, p.getBarcode());
		values.put(Product.QUANTITY, p.getQuantity());
		values.put(Product.WEIGHT, p.getWeight());
		values.put(Product.PRICE, p.getPrice());
		
		mActivity.getContentResolver().insert(
				BasketContentProvider.CONTENT_PRODUCT_URI, values);
	}



	@Override

	public Product Get(long id) {
		Cursor mCursor = mActivity.managedQuery(
				BasketContentProvider.CONTENT_PRODUCT_URI, mContent, null, null, null);

		if (mCursor.moveToFirst()) {
			do {
				if (mCursor.getLong(0) == id) {
					return new Product(mCursor.getLong(0), mCursor.getInt(2),
							mCursor.getString(3), mCursor.getString(4),
							mCursor.getInt(5), mCursor.getFloat(6),
							mCursor.getFloat(7));
				}
			} while (mCursor.moveToNext());
		}

		return null;
	}

	@Override
	public Basket LoadProduct(Basket basket) {
		
		Cursor mCursor = mActivity.managedQuery(
				BasketContentProvider.CONTENT_PRODUCT_URI, mContent, null, null, null);
		
		basket.getProducts().clear();
		
		if (mCursor.moveToFirst()) {
			do {
				if (mCursor.getLong(1) == basket.getId()) {
					basket.getProducts().add(
							new Product(mCursor.getLong(0), mCursor.getInt(2),
									mCursor.getString(3), mCursor.getString(4),
									mCursor.getInt(5), mCursor.getFloat(6),
									mCursor.getFloat(7)));
				}
			} while (mCursor.moveToNext());
		}

		return basket;
	}

	@Override
	public void Update(Product p) {
		ContentValues values = new ContentValues(1);

		values.put(Product.TYPE_OF_PRODUCT, p.getTypeOfProduct());
		values.put(Product.NAME, p.getName());
		values.put(Product.BARCODE, p.getBarcode());
		values.put(Product.QUANTITY, p.getQuantity());
		values.put(Product.WEIGHT, p.getWeight());
		values.put(Product.PRICE, p.getPrice());

		String searchId = Product.ID + "=" + p.getId();
		mActivity.getContentResolver().update(
				BasketContentProvider.CONTENT_PRODUCT_URI, values, searchId, null);
	}

	@Override
	public void Delete(long id) {
		String searchId = Product.ID + "=" + id;

		mActivity.getContentResolver().delete(
				BasketContentProvider.CONTENT_PRODUCT_URI, searchId, null);
	}

	public void SetActivity(Activity mActivity) {
		this.mActivity = mActivity;
	}
}
